<template>
  <div class="home">
    <el-container>
      <el-header>
        <img src="../assets/img/logo.png" alt />
        <h2>电商后台系统管理</h2>
        <el-button class="exit" @click="exit" type="info">退出</el-button>
      </el-header>
      <el-container>
        <el-aside :width="isCollapse?'64px':'230px'">
          <div class="toggle-auto" @click="isCollapse=!isCollapse">| | |</div>
          <el-menu
            router
            unique-opened
            class="el-menu-vertical-demo"
            background-color="#373d41"
            text-color="#fff"
            active-text-color="#ffd04b"
            :collapse="isCollapse"
            :collapse-transition="false"
            :default-active="flag || 'users'"
            @select="select"
          >
            <el-submenu :index="menus.id+''" v-for="menus in menusList" :key="menus.id">
              <!-- 一级菜单 -->
              <template slot="title">
                <i :class="icon[menus.id]"></i>
                <span>{{menus.authName}}</span>
              </template>

              <!-- 二级菜单 -->
              <el-menu-item :index="'/'+item.path" v-for="item in menus.children" :key="item.id">
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{item.authName}}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <!-- 子级漏油 -->
          <router-view />
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
           
<script>
export default {
  name: "home",
  data() {
    return {
      // 侧边栏数据
      menusList: [],
      // 侧边栏是否折叠
      isCollapse: false,
      // 列表图像
      icon: {
        125: "el-icon-user-solid",
        103: "el-icon-connection",
        101: "el-icon-goods",
        102: "el-icon-tickets",
        145: "el-icon-edit-outline"
      },
      // 当前选中的
      flag: ""
    };
  },
  methods: {
    exit() {
      // 退出登录
      window.localStorage.setItem("token_ds", "");
      this.$message({
        message: "退出成功",
        type: "success",
        duration: 1000,
        // 跳转首页
        onClose: () => {
          this.$router.push("/login");
        }
      });
    },
    getmenus() {
      // 发送请求--获取左侧菜单
      this.$http.get("/menus").then(res => {
        if (res.status == 200) {
          this.menusList = res.data.data;
        }
      });
    },
    select(key, keyPath) {
      // 点击时获取所点击的辨识值（index）===当前的地址信息
      sessionStorage.setItem("defaultActive", key);
    }
  },
  created() {
    // 初始化==获取左侧菜单
    this.getmenus();
    // 判读本地临时缓存是否存在
    // 存在----让默认的展开---并跳转到当前组件
    if (sessionStorage.getItem("defaultActive")) {
      this.flag = sessionStorage.getItem("defaultActive");
      this.$router.push(sessionStorage.getItem("defaultActive"));
    } else {
      // 不存在 ==则返回欢迎页
      this.$router.push("/home");
    }
  }
};
</script>
<style scoped lang='less'>
.home {
  .el-container {
    height: 100vh;
    overflow: hidden;
  }
  .el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    align-items: center;
    h2 {
      font-size: 20px;
      color: #fff;
    }
  }
  .el-aside {
    background-color: #373d41;
    .el-menu {
      border: none;
    }
    .toggle-auto {
      width: 100%;
      height: 30px;
      line-height: 30px;
      text-align: center;
      color: #fff;
      background-color: #4a5064;
      cursor: pointer;
    }
  }
  .el-main {
    background-color: #eaedf1;
  }
}
</style>